/*
 * This file is part of andExplorer, android.ovhoo.com
 * Copyright (C) 2007 Mohamed ZALIM <mzalim@ovhoo.com>
 *
 * andExplorer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * andExplorer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */

package com.ovhoo.android.file;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.StringTokenizer;

import com.ovhoo.android.fiveexplore.R;

import android.content.Context;


/**
 * This Hashtable maps file extensions to their mime type.
 * adaptation of Maxence Bernard MimeTypes class for muCommander see http://www.mucommander.com
 */
public class MimeTypes extends Hashtable<String, String> {
	private static MimeTypes uniqueInstance = null;
	private Context context;
    
    private MimeTypes(Context context) {
    	this.context = context;
    	
        BufferedReader br;

        br = null;
        try {
            br = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(R.raw.mimetypes)));

            String line;
            StringTokenizer st;
            String description;

            while((line=br.readLine())!=null) {
                try {
                    st = new StringTokenizer(line);
                    description = st.nextToken();

                    while(st.hasMoreTokens())
                        put(st.nextToken(), description);
                }
                catch(Exception e) {
                    // If a line contains an error, catch the exception and go to the next line
                }
            }
        }
        catch(IOException e) {}
        // Makes sure the stream is closed.
        // This might not be strictly necessary as streams on internal resources are a bit of an unknown,
        // but since the ClassLoader.getResourceAsStream documentation doesn't explicitly say that such
        // streams do not need closing, it's safer to assume they do.
        finally {
            if(br != null) {
                try {br.close();}
                catch(IOException e) {}
            }
        }
    }
	
    
    public static MimeTypes getInstance(Context context){
    	if (MimeTypes.uniqueInstance == null){
    		MimeTypes.uniqueInstance = new MimeTypes(context);
    	}
    	
    	return MimeTypes.uniqueInstance;
    }
	
	/**
     * Returns the MIME type of the given file (determined by the file extension), <code>null</code>
     * if the type is unknown (unknown or no extension) or if the file is a folder.
     */
    public String getMimeType(FileInterface file) {
        if(file.isDirectory())
            return null;
        
        String name = file.getName();
        int pos = name.lastIndexOf('.');
        if(pos==-1)
            return null;

        return uniqueInstance.get(name.substring(pos+1, name.length()).toLowerCase());
    }
    
    public int getFileIcon(FileInterface file){
    	
    	
		if (file == null){
			return R.drawable.unreadable;
		}
		
		if (file.isDirectory()){
			return R.drawable.folder;
		}
		else{
			
			String _mimeType = this.getMimeType(file);
			
			if (_mimeType == null){
				return R.drawable.generic;
			}
			else if (_mimeType.startsWith("audio")){
				return R.drawable.audio_generic; 
			}
			else if (_mimeType.startsWith("image")){
				return R.drawable.image; 
			}
			else if (_mimeType.startsWith("video")){
				return R.drawable.video; 
			}
			else if (_mimeType.startsWith("text")){
				return R.drawable.text; 
			}
			else if (_mimeType.startsWith("archive")){
				return R.drawable.package_generic; 
			}
			else{
				return R.drawable.generic;
			}
			
		}
	}

}
